// pages/Main/mainIndex/mainIndex.js
var common = require('../../../utils/common.js')
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: [],
    stunewdata: [],
    content: [],
    stunewdatas: [],
    array: ['努力', '合格', '良好', '优秀','无'],
    pickerindex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let c = common.Getorgid();
    this.setData({
      orgid: c.orgid,
      userid: c.userid,
      paperid: options.paperid,
      examid: options.examid,
      classid: options.classid,
    });
    console.log(this.data.examid)
    this.test();


  },
  //6.5.1 考试中的试卷题目列表
  test: function () {
    common.showLoading("加载中", "loading");
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.paperid, }
    businessLogic.Examination(params, (res) => {
      //console.log("6.5.1", res)

      this.student(res);
    })
  },
  //6.2.1
  student: function (timu) {
    this.setData({
      timu: timu
    });
    // console.log("timu",this.data.timu)
    let params = { "userid": this.data.userid, "orgid": this.data.orgid, "searchField": "departmentid,type", "searchValue": "" + this.data.classid + ",077ba48e-76f8-4afd-8a84-67b4f2ed3762" }
    businessLogic.QueryStudentlist(params, (res) => {
      if (res.infos == '' || res.infos == undefined) {
        common.hideLoading();
      }
      //console.log("6.2.1 name", res)
      var res = res.infos;
      this.setData({
        name: res
      });
      for (let k = 0; k <= this.data.name.length - 1; k++) {
        //6.5.2查询每个人在试卷中每道题目的情况
        let paramsa = {
          "orgid": this.data.orgid, "userid": this.data.userid, "searchField": 'peopleid,examid', "searchValue": "" + this.data.name[k].id + "," + this.data.examid + " "
        }
        businessLogic.TestCase(paramsa, (resa) => {

          // console.log("6.5.2 content", resa)
          this.setData({
            content: resa
          });
          let temp = [];
          let num = 0;
          let s = 0;
          for (var m = 0; m < this.data.timu.length; m++) {
            for (var n = 0; n < this.data.timu[m].qt.length; n++) {
              s++;
              if (this.data.timu[m].qt[n].typecode == '1ab96071-3741-40b7-983c-bba5a031fe0d') {
                num += this.data.timu[m].qt[n].csub.length;
              }
            }
          }
          var num2 = s + num;
          if (this.data.content.length == num2) {
            for (var m = 0; m < this.data.timu.length; m++) {
              for (var n = 0; n < this.data.timu[m].qt.length; n++) {
                for (var i = 0; i < this.data.content.length; i++) {//对应id的学生所做的题目数量
                  //判断对应id的学生所做的题目与试卷的题目id是否相等
                  if (this.data.timu[m].qt[n].id == this.data.content[i].testid) {
                    temp.push({ testid: this.data.content[i].testid, statemark: this.data.content[i].statemark });
                    this.data.name[k].qes = temp;
                  }
                }
              }
            }
            // console.log("temp",this.data.name)
          }
          //该生没有做这套试卷时
          else {
            for (var m = 0; m < this.data.timu.length; m++) {
              for (var n = 0; n < this.data.timu[m].qt.length; n++) {
                temp.push({ testid: '', statemark: 2 });
                this.data.name[k].qes = temp;
              }
            }
          }
          this.setData({
            stunewdata: this.data.name
          });
          //6.5.3 获取每个学生每张试卷的详情（批改情况）
          let paramsb = {
            "orgid": this.data.orgid, "userid": this.data.userid, "searchField": 'peopleid,examid', "searchValue": "" + this.data.name[k].id + "," + this.data.examid + " "
          }
          businessLogic.QueryPerson(paramsa, (resb) => {

            //console.log("6.5.3", resb)
            if (resb.length > 0) {
              this.data.stunewdata[k].statemark = resb[0].statemark;
              this.data.stunewdata[k].rank = resb[0].rank;
              this.data.stunewdata[k].thisid = resb[0].id;
            } else {
              this.data.stunewdata[k].statemark = 2;
             // this.data.stunewdata[k].rank = 4;
            }
            this.setData({
              stunewdatas: this.data.stunewdata
            });

            //console.log("stunewdatas", this.data.stunewdatas)
            //console.log("stunewdatas",this.data.stunewdatas[0].qes)
            //定义动态长度的数组，并赋值数组里面每个元素为0，方便计数
            var rightnum = new Array;
            for (let p = 0; p < this.data.stunewdatas[0].qes.length; p++) {
              rightnum[p] = 0;
            }
            //开始计数每道题正确的人数
            for (let i = 0; i < this.data.stunewdatas.length; i++) {
              for (let j = 0; j < this.data.stunewdatas[i].qes.length; j++) {
                if (this.data.stunewdatas[i].qes[j].statemark == 1) {
                  rightnum[j]++;
                }
              }
            }
            //将正确人数转成每道题的正确率
            for (var i = 0; i < rightnum.length; i++) {
              rightnum[i] = (rightnum[i] / this.data.name.length).toFixed(4) * 100
            }
            this.setData({
              rnum: rightnum
            })
            //将正确率放入二维数组，方便遍历
            var p = 0;
            for (var m = 0; m < this.data.timu.length; m++) {
              for (var n = 0; n < this.data.timu[m].qt.length; n++) {
                this.data.timu[m].qt[n].rnum = this.data.rnum[p]
                p++;
              }
              this.setData({
                topdata: this.data.timu
              })
              //console.log(this.data.topdata)
              common.hideLoading();
            }
          })
        })
      }
    })
  },
  //修改等级
  bindPickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.currentTarget.dataset.index
    var thisid = e.currentTarget.dataset.thisid
    let InfoJson = '{"classid":"97678a51-f702-4181-8750-881b72a1e665","orgid":"' + this.data.orgid + '","userid":"' + this.data.userid + '","rank":"' + e.detail.value + '","id":"' + thisid + '"}';
    let params = { "InfoJson": InfoJson, "userid": this.data.userid, "orgid": this.data.orgid, "classid": "97678a51-f702-4181-8750-881b72a1e665" };
    businessLogic.UpdateStaffTestnew(params, (res) => {
      console.log(res)
      this.test();
    })
  },
  change: function () {
    console.log(111);
    common.modalcnt("修改评分", "该学生评分不允许修改！")
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.test();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  classTest: function () {
    wx.navigateTo({
      url: "../classTest/classTest?id=" + this.data.examid + "&&paperid=" + this.data.paperid
    });
  },
})